
package de.swta.freezay;

import de.swta.freezay.admin.category.CategoryView;
import de.swta.freezay.admin.item.ItemView;
import de.swta.freezay.admin.reservation.ReservationView;
import de.swta.freezay.admin.unit.UnitView;
import de.swta.freezay.base.BasePage;
import de.swta.freezay.base.LoginPanel;
import de.swta.freezay.base.LogoutPanel;
import de.swta.freezay.components.PackageView;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.Component;
import org.apache.wicket.extensions.ajax.markup.html.AjaxLazyLoadPanel;
import org.apache.wicket.extensions.markup.html.tabs.AbstractTab;
import org.apache.wicket.extensions.markup.html.tabs.TabbedPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;

/**
 * @author Jan Korreck
 * @author Christian Baeumer
 */
public class Home extends BasePage {

    /** Factory to create some tab-components */
    private static final class TabFactory {
        // <editor-fold defaultstate="collapsed" desc="...">

        /** The loadingComponent is displayed while data from databse is read */
        private static Component loadingComponent;

        /**
         * Creates the PackageTab (if necessary) and returns it
         * @return the PackageTab
         */
        private static final AbstractTab getPackageTab() {
            return new AbstractTab(new Model("Pakete")) {
                // <editor-fold defaultstate="collapsed" desc="...">
                public Panel getPanel(String panelId) {
                    /* to display a message while connecting to database and reading
                     * the package data a lazyLoadPanel is used */
                    return new AjaxLazyLoadPanel(panelId) {

                        @Override
                        public Component getLazyLoadComponent(String id) {
                            /* load packge view */
                            return new PackageView(id);
                        }

                        @Override
                        public Component getLoadingComponent(String markupId) {
                            return TabFactory.getLoadingComponent(
                                    super.getLoadingComponent(markupId));
                        }
                    };
                }
                // </editor-fold>
            };
        }

        /**
         * Creates the ReservationTab (if necessary) and returns it
         * @return the ReservationTab
         */
        private static final AbstractTab getReservationTab() {
            return new AbstractTab(new Model("Reservierungen")) {
                // <editor-fold defaultstate="collapsed" desc="...">
                public Panel getPanel(String panelId) {
                    /* to display a message while connecting to database and
                     * reading the reservation data a lazyLoadPanel is used */
                    return new AjaxLazyLoadPanel(panelId) {

                        @Override
                        public Component getLazyLoadComponent(String id) {
                            /* load packge view */
                            return new ReservationView(id);
                        }

                        @Override
                        public Component getLoadingComponent(String markupId) {
                            return TabFactory.getLoadingComponent(
                                    super.getLoadingComponent(markupId));
                        }
                    };
                }
                // </editor-fold>
            };
        }

        /**
         * Creates the ItemTab (if necessary) and returns it
         * @return the ItemTab
         */
        private static final AbstractTab getItemTab() {
           return new AbstractTab(new Model("Produkte")) {
                // <editor-fold defaultstate="collapsed" desc="...">
                public Panel getPanel(String panelId) {
                    /* to display a message while connecting to database and
                     * reading the item data a lazyLoadPanel is used */
                    return new AjaxLazyLoadPanel(panelId) {

                        @Override
                        public Component getLazyLoadComponent(String id) {
                            /* load packge view */
                            return new ItemView(id);
                        }

                        @Override
                        public Component getLoadingComponent(String markupId) {
                            return TabFactory.getLoadingComponent(
                                    super.getLoadingComponent(markupId));
                        }
                    };
                }
                // </editor-fold>
            };
        }

        /**
         * Creates the CategoryTab (if necessary) and returns it
         * @return the CategoryTab
         */
        private static final AbstractTab getCategoryTab() {
            return new AbstractTab(new Model("Kategorien")) {
                 // <editor-fold defaultstate="collapsed" desc="...">
                public Panel getPanel(String panelId) {
                    /* to display a message while connecting to database and
                     * reading the category data a lazyLoadPanel is used */
                    return new AjaxLazyLoadPanel(panelId) {

                        @Override
                        public Component getLazyLoadComponent(String id) {
                            /* load packge view */
                            return new CategoryView(id);
                        }

                        @Override
                        public Component getLoadingComponent(String markupId) {
                            return TabFactory.getLoadingComponent(
                                    super.getLoadingComponent(markupId));
                        }
                    };
                }
                // </editor-fold>
            };
        }

        /**
         * Creates the UnitTab (if necessary) and returns it
         * @return the UnitTab
         */
        private static final AbstractTab getUnitTab() {
            return new AbstractTab(new Model("Einheiten")) {
                // <editor-fold defaultstate="collapsed" desc="...">
                public Panel getPanel(String panelId) {
                    /* to display a message while connecting to database and
                     * reading the unit data a lazyLoadPanel is used */
                    return new AjaxLazyLoadPanel(panelId) {

                        @Override
                        public Component getLazyLoadComponent(String id) {
                            /* load packge view */
                            return new UnitView(id);
                        }

                        @Override
                        public Component getLoadingComponent(String markupId) {
                            return TabFactory.getLoadingComponent(
                                    super.getLoadingComponent(markupId));
                        }
                    };
                }
                // </editor-fold>
            };
        }

        /**
         * Creates the LoadingComponent (if necessary) and returns it
         * @return the LoadingComponent
         */
        private static final Component getLoadingComponent(
                final Component parentComponent) {
            // <editor-fold defaultstate="collapsed" desc="...">

            if (loadingComponent == null) {
                /* create the loading component */
                loadingComponent = parentComponent;
                loadingComponent.setDefaultModelObject("<p>&nbsp;&nbsp;"
                        + "Datenbankverbindung wird aufgebaut... "
                        + loadingComponent.getDefaultModelObjectAsString());

            }

            /* update the markup id */
            loadingComponent.setMarkupId(parentComponent.getMarkupId());

            // </editor-fold>
            return loadingComponent;
        }

        // </editor-fold>
    }


    public Home() {

        super("Home");

        final List tabs = new ArrayList();

        /* common tab-panel: PackageTab */
        tabs.add(TabFactory.getPackageTab());


        if (((FreezaySession) getSession()).isLoggedIn()) {
            /* admin logged in: add some edit-tabs */

            /* ReservationTab */
            tabs.add(TabFactory.getReservationTab());
            /* ItemTab */
            tabs.add(TabFactory.getItemTab());
            /* CategoryTab */
            tabs.add(TabFactory.getCategoryTab());
            /* UnitTab */
            tabs.add(TabFactory.getUnitTab());

            /* admin is logged in: display LOGOUT-COMPONENT */
            add(new LogoutPanel("loginLogout"));

        } else {
            /* admin not logged in: display LOGIN-COMPONENT */
            add(new LoginPanel("loginLogout"));
        }

        /* add a TabbedPanel with the created tabs */
        add(new TabbedPanel("tabs", tabs));
        
    }

}
